<template>
  <div class="app-container">
    <el-input
      v-model="search"
      class="search-main"
      style="margin-bottom:20px"
      placeholder="请输入病人姓名"
      prefix-icon="el-icon-search"
      @keyup.enter.native="handleSelect"
    />
    <el-table
      :data="prescriptionList"
      style="width: 100%;"
      border
      height="40vh"
      highlight-current-row
      @row-click="clickRow"
    >
      <el-table-column
        align="center"
        label="处方编号"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="医师姓名"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.adminName }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="病人姓名"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="病因"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.pathogeny }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="处方价格"
        width=""
      >
        <template slot-scope="scope">
          ￥{{ scope.row.amount }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="创建时间"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="用药开始时间"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.medicationTimeStart }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="用药结束时间"
      >
        <template slot-scope="scope">
          {{ scope.row.medicationTimeEnd }}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :current-page="pageNum"
      :page-size="pageSize"
      layout="total,sizes,  prev, pager, next, jumper"
      :total="totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <div style="text-align:right;">
      <el-button
        type="danger"
        @click="handleCancel"
      >取消</el-button>
      <el-button
        type="primary"
        @click="handleConfirm"
      >确定</el-button>
    </div>
  </div>
</template>

<script>
import { getPrescription, getPrescriptionInfoById } from '@/api/prescription'
import { timestampToTimeDay, timestampToTime } from '@/utils/time'

export default {
  props: {
    type: {
      type: String,
      default() {
        return ''
      }
    },
    payment: {
      type: String,
      default() {
        return ''
      }
    }
  },
  data() {
    return {
      routes: [],
      prescriptionList: [],
      dialogVisible: false,
      checkStrictly: false,
      prescription: {},
      selectPrescription: {},
      search: '',
      pageNum: 1, // 默认每页显示1条数据
      pageSize: 10,
      loading: false,
      totalCount: 0
    }
  },
  created() {
    this.getPrescription({
      pageNum: this.pageNum,
      pageSize: this.pageSize,
      type: this.type,
      payment: this.payment
    })
  },
  methods: {
    async getPrescription(data) {
      const res = await getPrescription(data)
      const list = res.data.list
      console.log(list)
      for (const i in list) {
        list[i].medicationTimeStart = timestampToTimeDay(list[i].medicationTimeStart)
        list[i].medicationTimeEnd = timestampToTimeDay(list[i].medicationTimeEnd)
        list[i].createTime = timestampToTime(list[i].createTime)
      }
      this.prescriptionList = list
      this.totalCount = res.data.total
    },
    handleDetail(scope) {
      this.dialogVisible = true
      this.prescription = scope.row
    },
    handleSizeChange(page) {
      this.pageSize = page
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    handleCurrentChange(page) {
      this.pageNum = page
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    clickRow(row) {
      this.selectPrescription = row
    },
    handleSelect() {
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        type: this.type,
        payment: this.payment,
        username: this.search
      })
    },
    async handleConfirm() {
      if (!this.selectPrescription.id) {
        this.$message({
          message: '选择一个药方'
        })
        return
      } else {
        console.log(this.selectPrescription)
        const { data } = await getPrescriptionInfoById({ id: this.selectPrescription.id })
        console.log(data)
        this.$emit('confirm', data)
      }
    },
    handleCancel() {
      this.$emit('cancel', this.selectPrescription)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
.el-table__body tr.current-row > td {
  background-color: rgb(64, 158, 255) !important;
  color: #fff;
}
</style>
